<template>
	<view class="app-page">
		<view class="app-page-list">
			<view class="app-page-item" v-for="(item, index) in applist" :key="index" @click="getAppinfo(item)">
				<view class="app-header">
					<image class="app-icon" :src="item.appIcon" mode="widthFix" />
					<view class="app-name">
						<text class="app-title">{{ item.appName }}</text>
						<text class="app-package">{{ item.packageName }}</text>
					</view>
				</view>
				<view class="info-row">
					<text class="label">版本名称：</text>
					<text class="value">{{ item.versionName }}</text>
				</view>
				<view class="info-row">
					<text class="label">版本号：</text>
					<text class="value">{{ item.versionCode }}</text>
				</view>
				<view class="info-row">
					<text class="label">是否系统应用：</text>
					<text class="value">{{ item.systemApp ? '是' : '否' }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getInstalledApps
	} from '../../uni_modules/wen-getapp';

	export default {
		data() {
			return {
				applist: []
			};
		},
		async created() {
			const res = await getInstalledApps();
			this.applist = res.map(app => {
				if (app.appIcon && !app.appIcon.startsWith('data:image')) {
					app.appIcon = 'data:image/png;base64,' + app.appIcon;
				}
				return app;
			});
		},
		methods: {
			getAppinfo(item) {
				uni.navigateTo({
					url: '/pages/appinfo/appinfo?item=' + JSON.stringify(item)
				})
			},
		}
	};
</script>

<style scoped lang="scss">
	.app-page {
		padding: 20rpx;
		background-color: #f0f2f5;
	}

	.app-page-list {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.app-page-item {
		width: 90%;
		margin: 20rpx 0;
		padding: 30rpx;
		border-radius: 20rpx;
		background-color: #ffffff;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		display: flex;
		flex-direction: column;
	}

	.app-header {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.app-icon {
		width: 100rpx;
		height: 100rpx;
		border-radius: 20rpx;
		margin-right: 20rpx;
		background-color: #f2f2f2;
	}

	.app-name {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.app-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.app-package {
		font-size: 26rpx;
		color: #999;
		word-break: break-all;
	}

	.copy-btn {
		font-size: 24rpx;
		color: #ffffff;
		background-color: #007aff;
		padding: 10rpx 20rpx;
		border-radius: 10rpx;
		line-height: 1;
		margin-left: 10rpx;
	}

	.info-row {
		display: flex;
		justify-content: space-between;
		margin-top: 12rpx;
		font-size: 26rpx;

		.label {
			font-weight: 500;
			color: #666;
		}

		.value {
			color: #444;
		}
	}
</style>